package com.dateview.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;

import com.dateview.test.adapter.ExpandableAdapter;
import com.dateview.test.util.DateUtil;
import com.dateview.test.view.DateView;
import com.dateview.test.view.OnChildClickListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private SparseArray<List<String>> mSparseArray;

    private DateView mDateView;

    private ExpandableAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDateView = (DateView) findViewById(R.id.dv_date_view);


        initData();
        mAdapter = new ExpandableAdapter(this, mSparseArray);
        mDateView.setAdapter(mAdapter);
        //设置全部展开，一定要到setAdapter以后
        mDateView.setIsExpandAll(true);

        mAdapter.setOnChildClickListener(mChildClickListener);
    }

    private OnChildClickListener mChildClickListener = new OnChildClickListener() {
        @Override
        public void onChildClick(View parent, View v, int groupPosition, int childPosition, long id) {
            String month = String.valueOf(mSparseArray.keyAt(groupPosition) + 1);
            Log.e("MainActivity", "2016年" + month + "月" + mSparseArray.get(groupPosition).get(childPosition) + "日");
        }
    };

    private void initData() {
        mSparseArray = new SparseArray<>();
        for (int i = 0; i < 12; i++) {
            List<String> mStrings = getItemListData(i + 1);
            mSparseArray.put(i, mStrings);
        }
    }

    /**
     * 根据月份获取item数据排列
     *
     * @param month
     */
    private List<String> getItemListData(int month) {
        List<String> mStrings = new ArrayList<>();
        int weekDayPos = DateUtil.getTheFirstWeekDayInAMonth(month);
        //添加空字符串
        for (int i = 0; i < weekDayPos; i++) {
            mStrings.add("");
        }
        //添加空日期项
        int monthDays = DateUtil.getDaysByMonth(month);
        for (int i = 0; i < monthDays; i++) {
            mStrings.add(String.valueOf(i + 1));
        }

        return mStrings;
    }

}
